package com.lisp.springframework.core.aop.springaop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class PrintAspect {

    @Pointcut("execution (public * toString(..))")
    public void print() { }
    
    @Pointcut("within(com.lisp.springframework.core.aop.springaop..*)")
    public void withInAopPackage() { }
    
    @Pointcut("print() && withInAopPackage()")
    public void aopPrint() { }
    
    @Before("com.lisp.springframework.core.aop.springaop.PrintAspect.aopPrint()")
    public void beforePrint() {
        System.out.println("before print advice executed");
    }
    
    @AfterReturning(returning = "retVal",
            pointcut = "execution (public * toString(..)) && within(com.lisp.springframework.core.aop.springaop..*)")
    public void afterPrint(String retVal) {
        System.out.println("after print advice executed \n\t" + retVal);
    }
    
    @Around("com.lisp.springframework.core.aop.springaop.PrintAspect.aopPrint()")
    public Object aroundPrint(ProceedingJoinPoint pjp) throws Throwable {
        long current = System.currentTimeMillis();
        Object retVal = pjp.proceed();
        long usedTime = System.currentTimeMillis() - current;
        System.out.println("aroud print advice executed " + usedTime); 
        return retVal;
    }
}
